package com.zhp.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @date 2023/4/11
 * @desc  使用数据库连接池
 *
 *
 * ThreadLocal
 */
public class DbUtils {
    // 声明Druid连接池对象
    private static DruidDataSource druidDataSource;

    private static  final  ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();
    static {
        try {

            Properties properties = new Properties( );
            // 将配置文件读作输入流
            InputStream stream = DbUtils.class.getResourceAsStream("/db.properties");
            // properties对象加载流,即配置信息都在properties对象里面
            properties.load(stream);
            // 通过配置信息创建出druidDataSource数据源
            druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace( );
        }
    }


    public static DataSource getDataSource(){
        return druidDataSource;
    }

    /**
     * 通过DruidDataSource获得连接
     */
    public static Connection getConnection() {

        Connection conn = THREAD_LOCAL.get();
        try {
            // 2 获得连接
            if(conn ==null){
                conn = druidDataSource.getConnection( );
                THREAD_LOCAL.set(conn);
            }
        } catch (Exception e) {
            e.printStackTrace( );
        }
        return conn;
    }

    //开启事务   关闭自动提交
    public static void begin(){
        Connection connection = getConnection();

        try {
            //关闭自动提交
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    // 提交事务
    public static void commit(){
        Connection connection = getConnection();
        try {
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,null,null);
        }
    }


    public static void rollback(){
        Connection connection = getConnection();

        try {
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,null,null);
        }
    }


    /**
     * 关闭连接
     */
    public static void closeAll(Connection conn, Statement s) {
        if (conn != null) {
            try {
                conn.close( );
            } catch (SQLException e) {
                e.printStackTrace( );
            }
        }
        if (s != null) {
            try {
                s.close( );
            } catch (SQLException e) {
                e.printStackTrace( );
            }
        }
    }

    public static void closeAll(Connection conn, Statement s, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close( );
            } catch (SQLException e) {
                e.printStackTrace( );
            }
        }
        if (s != null) {
            try {
                s.close( );
            } catch (SQLException e) {
                e.printStackTrace( );
            }
        }
        if (rs != null) {
            try {
                rs.close( );
            } catch (SQLException e) {
                e.printStackTrace( );
            }
        }
    }


    public static void main(String[] args) {

        System.out.println(getConnection());
        System.out.println(getConnection());
    }
}
